#include <stdio.h>
#include <stdlib.h>


int* relativeSortArray(int* arr1, int arr1Size, int* arr2, int arr2Size, int* returnSize) {
    int* hash = (int*)calloc(sizeof(int), 1001);
    int* ans = (int*)malloc(sizeof(int) * (arr1Size + 1));
    for (int i = 0; i < arr1Size; i++)
    {
        hash[arr1[i]]++;
    }

    int j = 0;
    for (int i = 0; i < arr2Size; i++)
    {
        while (hash[arr2[i]] != 0)
        {
            ans[j++] = arr2[i];
            hash[arr2[i]]--;
        }
    }

    for (int i = 0; i < 1001; i++)
    {
        while (hash[i] != 0)
        {
            ans[j++] = i;
            hash[i]--;
        }
    }
    *returnSize = j;
    return ans;
}


int main()
{
    int s1[] = { 2,3,1,3,2,4,6,7,9,2,19 };
    int s2[] = { 2,1,4,3,9,6 };
    int i = 0;
    int* ret = relativeSortArray(s1, 11, s2, 6, &i);
    for (int j = 0; j < i; j++)
    {
        printf("%d ", ret[j]);
    }
    return 0;
}